
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;

namespace Tathata.Web.Administration
{
    public partial class Administration_DiscountDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Discount discount = DiscountManager.GetByDiscountID(this.DiscountID);
            if (discount != null)
            {
                this.txtName.Text = discount.Name;
                this.cbUsePercentage.Checked = discount.UsePercentage;
                this.txtDiscountPercentage.Value = discount.DiscountPercentage;
                this.txtDiscountAmount.Value = discount.DiscountAmount;
                this.cStartDateButtonExtender.SelectedDate = discount.StartDate;
                this.cEndDateButtonExtender.SelectedDate = discount.EndDate;
            }
            else
                Response.Redirect("Discounts.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    DateTime discountStartDate = DateTime.MinValue;
                    DateTime discountEndDate = DateTime.MinValue;
                    if (!DateTime.TryParse(txtStartDate.Text, out discountStartDate))
                        throw new Exception("Start date is not set");
                    if (!DateTime.TryParse(txtEndDate.Text, out discountEndDate))
                        throw new Exception("End date is not set");
                    Discount discount = DiscountManager.GetByDiscountID(this.DiscountID);

                    if (discount != null)
                    {
                        discount = DiscountManager.UpdateDiscount(discount.DiscountID, txtName.Text, cbUsePercentage.Checked,
                           txtDiscountPercentage.Value,
                           txtDiscountAmount.Value, discountStartDate,
                           discountEndDate, discount.Deleted);
                        Response.Redirect("DiscountDetails.aspx?DiscountID=" + discount.DiscountID.ToString());
                    }
                    else
                        Response.Redirect("Discounts.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                DiscountManager.MarkDiscountAsDeleted(this.DiscountID);
                Response.Redirect("Discounts.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        public int DiscountID
        {
            get
            {
                return CommonHelper.QueryStringInt("DiscountID");
            }
        }
    }
}
